<template>
  <div class="index-div">
    <div class="conter-div" :class="{ 'mobile_view': isMobileView }">
      <div class="imgAndDlondTw" ref="imgAndDlondTwRef">
        <div class="imgAndDlond">
          <div class="zhezhao" v-show="ispaly">
            <div @click="playVideo">
              <img src="@/assets/img/play.png" alt="" >
              <div class="begin-stu">开始学习</div>
            </div>
          </div>
          <video ref="videoRef" :src="videoUrl" @timeupdate="onTimeUpdate" :controls="!ispaly" poster="@/assets/img/bannerpc.png" ></video>
        </div>
        <div class="jindu" v-if="!isMobileView">
          <h2>2022客观题冲刺密卷班</h2>
          <div class="disf progress">学习进度<el-progress :percentage="progress" color="red" text-color="red"></el-progress></div>
          <div class="zan disf">
            <div  @click="iszan = !iszan" class="disf" :class="{action:iszan}">
              <img v-show="!iszan" src="@/assets/img/zan.png" alt="">
              <img v-show="iszan" src="@/assets/img/zan1.png" alt="">
              <span>{{ iszan ? '赞' : '1'}}</span>
            </div>
            <div @click="isshoucang = !isshoucang" class="disf" :class="{action:isshoucang}">
              <img v-show="!isshoucang" src="@/assets/img/shoucang.png" alt="" >
              <img v-show="isshoucang" src="@/assets/img/shoucang1.png" alt="" >
              <span>{{ isshoucang ? '未收藏' : '已收藏'}}</span>
            </div>
          </div>
          <div style="flex: 1;"></div>
          <el-button type="danger" round @click="commentClick" style="background-color:#D2362A">评论</el-button>
        </div>
        <div class="jindu" v-if="isMobileView">
          <h2>2022客观题冲刺密卷班</h2>
          <div class="disf juSB">
            <div class="disf progress">学习进度<el-progress :percentage="progress" color="red" text-color="red"></el-progress></div>
            <div class="zan disf">
              <div  @click="iszan = !iszan" class="disf" :class="{action:iszan}">
                <img v-show="!iszan" src="@/assets/img/zan.png" alt="">
                <img v-show="iszan" src="@/assets/img/zan1.png" alt="">
                <span>{{ iszan ? '赞' : '1'}}</span>
              </div>
              <div @click="isshoucang = !isshoucang" class="disf" :class="{action:isshoucang}">
                <img v-show="!isshoucang" src="@/assets/img/shoucang.png" alt="" >
                <img v-show="isshoucang" src="@/assets/img/shoucang1.png" alt="" >
                <span>{{ isshoucang ? '未收藏' : '已收藏'}}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      title="评论"
      :visible.sync="dialogVisible"
      
      width="40%"
      :before-close="handleClose">
      <el-input
        type="textarea"
        :autosize="{ minRows: 4, maxRows: 8}"
        placeholder="发布你的评论"
        v-model="textarea2">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="releaseClick" class="release">发布</el-button>
      </span>
    </el-dialog>
    <gyTest ref="gyTestRefs" :offsetWidth="offsetWidth" class="gyTest-div" :list1="list1"></gyTest>
  </div>
  </template>
  
  <script>
 import gyTest from './gyTest.vue'
  export default {
    name: 'HelloWorld',
    props: {
      msg: String,
    },
    components:{gyTest},
    data(){
      return{
        videoUrl:'http://txvideo.aijiayou.com/e0e86d1fvodgzp1252425236/97531f935576678019782313439/DhQnQyAkTXoA.mp4',
        ispaly:true,
        iszan:true,
        isshoucang:true,
        progress:0,
        isMobileView: false, // 用于标记当前是否为移动视图
        offsetWidth:1920,//页面宽
        screenWidth: null, // 屏幕宽度(监听屏幕宽度实时计算主图然后去算画中画大小按比例缩放)
        timeout:null,
        dialogVisible: false,
        textarea2:'',
        list1:[]
      }
    },
    watch:{
      screenWidth: {
        handler: function (val) {
          this.isMobileView = val <= 768;
          this.debounce(this.savePiptempObj, 200)
        },
        immediate: false
      }
    
  },
  mounted() {
   this.offsetWidth = this.$refs.imgAndDlondTwRef.offsetWidth
    this.handleWindowSizeChange(); // 初始化时检查一次
    this.screenWidth = document.body.clientWidth
    window.onresize = () => {
      return (() => {
        this.screenWidth = document.body.clientWidth
      })()
    }
    
  },
    methods:{
      // 防抖函数
      debounce(fn, delay) {
        if (this.timeout) clearTimeout(this.timeout)
        this.timeout = setTimeout(() => { fn && fn() }, delay)
      },
      // 获取应有的的宽度
        // 监听窗口大小变化
      savePiptempObj() {
        this.$nextTick(()=>{
          this.offsetWidth = this.$refs.imgAndDlondTwRef.offsetWidth
        })
      },
      // 初始化获取宽度判断是否为移动端
      handleWindowSizeChange() {
        // 判断窗口宽度来决定是否为移动视图
        this.offsetWidth = this.$refs.imgAndDlondTwRef.offsetWidth
        this.isMobileView = window.innerWidth <= 768;
        console.log(window.innerWidth,this.isMobileView);
      },
      playVideo(){
        this.$refs.videoRef.play()
        this.ispaly = false
      },
        // 播放进度更新时触发
      onTimeUpdate() {
        // currentTime 属性表示当前播放时间（秒）
        const currentTime = this.$refs.videoRef.currentTime;
        // duration 属性表示视频总时长（秒）
        const duration = this.$refs.videoRef.duration;
        // 根据需要处理或显示播放进度，例如：
        console.log(`当前播放进度: ${currentTime} / 总时长: ${duration}`);
        // 可以将进度更新到Vue数据属性，用于动态显示进度条等
        let progress = parseInt((currentTime / duration) * 100);
        if (progress>this.progress) {
          this.progress = progress
        }
      },
      // 发布
      releaseClick(){
        this.$refs.gyTestRefs.add({
          url:require('../assets/tx.png'),
          name:'新用户',
          time: this.$moment().format('YYYY-MM-DD'),
          content: this.textarea2
        })
        this.dialogVisible = false;
      },
        // 评论
      commentClick(){
        this.textarea2 = '';
        this.dialogVisible = true;
      },
      // 关闭弹窗
      handleClose(){
        this.textarea2 = '';
        this.dialogVisible = false;
      },
    },
    beforeDestroy() {
      // window.removeEventListener('resize', this.handleWindowSizeChange); // 组件销毁前移除监听器
    },
  }
  </script>
  
  <style scoped lang="less">
  .index-div{
    width: 100%;
    margin: 0 auto;
    height: 100vh;
  .conter-div{
    color: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    width: 60%;
    margin: 0 auto;
    .imgAndDlondTw{
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
    }
    .imgAndDlond{
      // width: 100vh;
      width: 80%;
      position: relative;
      .zhezhao{
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 10;
        display: flex;
        align-items: center;
        justify-content: center;
        img{
          cursor: pointer;
        }
        .begin-stu{
          color: #fff;
        }
      }
      video{
      height: 512px;
      width: 100%;
      object-fit: fill;
    }
    }
    .jindu{
      height: calc(516px - 25px);
      padding: 10px 10px 15px;
      // width: 40vh;
      width: 20%;
      background-color: #000;
      color: #fff;
      display: flex;
      flex-direction: column;
      h2{
        color: #fff;
      }
      .progress{
        font-size: 12px;
        ::v-deep .el-progress{
          flex: 1;
          margin-left: 10px;
          color: red !important;
        }
      }
      .zan{
        font-size: 12px;
        margin-top: 20px;
        div{
          border: 1px solid #fff;
          border-radius: 50px;
          cursor: pointer;
          padding: 4px 15px;
          text-align: center;
          margin-right: 10px;
          color: #cdcdcd;
          img{
            width: 20px;
            height: 20px;
          }
        }
        .action{
          color: red;
          border: 1px solid red;
        }
      }
    }
    
  }
  // 移动端
  .mobile_view{
   display: block;
   width: 100%;
    .imgAndDlondTw{
      flex-direction: column;
    }
    .imgAndDlond{
      width: 100%;
      video{
        height: auto;
      }
    }
    .jindu{
      background-color: #fff;
      height: auto;
      width: calc(100% - 20px);
      padding: 0 10px;
      color: #000;
      h2{
        color: #000;
        margin-bottom: 10px;
      }
      .progress{
        flex: 1;
      }
      .zan{
        margin: 0;
        flex: 1;
        justify-content: flex-end;
        div{
          margin-right: 0;
          margin-left: 10px;
          border-color: #cdcdcd;
          padding: 2px 11px;
        }
      }
    }
  }
  .disf{
    display: flex;
    align-items: center;
  }
  .juSB{
    justify-content: space-between;
  }
  .gyTest-div{
    width: 100%;background-color: #efefef;padding-top: 30px;margin-top: 30px;
    height: calc(100vh - 576px);
    // height: calc(516px + 245px);
  }
  // 弹窗
  ::v-deep{
    .el-dialog__header{
      border-bottom: 1px solid #cdcfd3;
    }
    .el-textarea__inner{
      border: 1px solid #D2362A;
    }
    .el-dialog__header{
      text-align: center;
    }
  }
  .release{
    background-color: #D2362A;
    color: #fff;
  }
  }
  </style>
  